import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/*
 *@Student 张策
 */
public class ReflectClassDemo {
    public static void reflectClassDemo(){
        Class<?>c1;
        try {
            c1=Class.forName("Student");
            Student student = (Student) c1.newInstance();
            System.out.println(student);
        }catch (ClassNotFoundException e){
            e.printStackTrace();
        } catch (InstantiationException e) {
            throw new RuntimeException(e);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        }
    }
    public static void reflectPrivateConstructor(){
        Class<Student> c1=null;
        try {
                c1 = (Class<Student>) Class.forName("Student");
            Constructor<Student> declaredConstructor = c1.getDeclaredConstructor(String.class, int.class);
            declaredConstructor.setAccessible(true);
            Student student = declaredConstructor.newInstance("我", 10);
            System.out.println(student);
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        } catch (NoSuchMethodException e) {
            throw new RuntimeException(e);
        } catch (InvocationTargetException e) {
            throw new RuntimeException(e);
        } catch (InstantiationException e) {
            throw new RuntimeException(e);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        }
    }
    public static void reflectPrivateField(){
        Class<Student> c1=null;
        try {
            c1=(Class<Student>) Class.forName("Student");
            Field field = c1.getDeclaredField("name");
            field.setAccessible(true);
            Student student =  c1.newInstance();
            field.set(student,"666");
            System.out.println(student);
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        } catch (NoSuchFieldException e) {
            throw new RuntimeException(e);
        } catch (InstantiationException e) {
            throw new RuntimeException(e);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        }
    }
    public static void reflectPrivateMethod(){
        Class<Student>c1=null;
        try {
            c1=(Class<Student>) Class.forName("Student");
            Method function = c1.getDeclaredMethod("function", String.class);
            function.setAccessible(true);
            Student student = c1.newInstance();
            function.invoke(student,"666");
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        } catch (InvocationTargetException e) {
            throw new RuntimeException(e);
        } catch (NoSuchMethodException e) {
            throw new RuntimeException(e);
        } catch (InstantiationException e) {
            throw new RuntimeException(e);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        }
    }
    public static void main(String[] args) throws ClassNotFoundException {
//        reflectClassDemo();
//        reflectPrivateConstructor();
        reflectPrivateField();
//        reflectPrivateMethod();
//        Student student = new Student();
        Student student = new Student();
        ClassLoader classLoader = student.getClass().getClassLoader();
        classLoader.loadClass("Student");
        Class<Integer> integerClass = int.class;
        Class<Integer> integerClass1 = Integer.TYPE;
        Class<Integer> integerClass2 = Integer.class;
    } 
}
